﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.Gateway;

namespace Wicture.QPZS.Test
{
    public class Brand
    {
        public string brandCode { get; set; }
    }
    public class VinQueryModel : Brand
    {
        public string vin { get; set; }
    }

    public class VinSubGroupQueryModel : Brand
    {
        public string vin { get; set; }
        public string id { get { return $"{mainGroupCode}_{modelId}"; } }

        public int modelId { get; set; }
        public int mainGroupCode { get; set; }
    }

    public static class Consts
    {
        public static string GetPath()
        {
            var pathSeg = Directory.GetCurrentDirectory();
            int index = pathSeg.IndexOf("Wicture.QPZS.Test");
            return pathSeg.Substring(0, index);
        }

    }

    public static class ConnectionHelper
    {
        private static JObject secret = JObject.Parse(File.ReadAllText($@"{Consts.GetPath()}\Wicture.QPZS.Test\secret.json"));
        public static string Get(string brandCode)
        {
            return secret.Value<string>(brandCode);
        }
    }

    public class TestDbGateway : IDbGateway
    {
        public DatabaseConnection Process(HttpContext context, JToken param, IdentityInfo userInfo)
        {
            var brandCode = param.Value<string>("brandCode");
            if (string.IsNullOrEmpty(brandCode)) return null;

            var connString = ConnectionHelper.Get(brandCode);

            return new DatabaseConnection
            {
                ReadConnectionString = connString,
                WriteConnectionString = connString
            };
        }
    }

    public class SubGroupTestCase
    {
        public bool pass { get; set; }
        public VinSubGroupQueryModel input { get; set; }
        public JArray outputs { get; set; }
    }

    public class VinPartQueryModel : Brand
    {
        public string vin { get; set; }
        public string id { get; set; }
    }

    public class PartTestCase
    {
        public bool pass { get; set; }
        public VinPartQueryModel input { get; set; }
        public JArray outputs { get; set; }
    }
}
